package cn.com.sinosoft.testxmltransform;

import java.util.Properties;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;


/**
 * WebService工具类
 * @author xiaoxiong
 *
 */
public class WSUtil {

	private static JaxWsProxyFactoryBean dictProxy;	//	字典信息推送服务客户端代理
	private static JaxWsProxyFactoryBean medicineProxy;	//	草药信息推送服务客户端代理
	
	private static String dictServiceUrl;
	private static String medicineServiceUrl;
	static{
		dictServiceUrl = getApplicationParam("dictServiceUrl");
		medicineServiceUrl = getApplicationParam("medicineServiceUrl");
	}
	
	private static Properties props;
	
	public static String getApplicationParam(String strParam) {
		//Properties props = null;
		if (props == null) {
			try {
				Resource resource = new ClassPathResource(
						"/web/conn.properties");
				props = PropertiesLoaderUtils.loadProperties(resource);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return props.getProperty(strParam);
	}
	
	/**
	 * 获取推送字典信息服务客户代理
	 * @return 字典信息服务客户代理
	 */
	public static PushDictService getPushDictClient() {
		Object service = createClientProxy(
				dictProxy, dictServiceUrl, PushDictService.class);
		return (PushDictService)service;
	}
	
	/**
	 * 获取推送草药信息服务客户代理
	 * @return 草药信息服务客户代理
	 */
	public static PushMedicineService getPushMedicineClient() {
		Object service = createClientProxy(
				medicineProxy, medicineServiceUrl, PushMedicineService.class);
		return (PushMedicineService)service;
	}
	
	/**
	 * 创建客户端服务代理
	 * @param proxy	WebService代理类
	 * @param serviceUrl	服务地址
	 * @param serviceClass	接口服务类
	 * @return	客户端服务代理
	 */
	private static Object createClientProxy(JaxWsProxyFactoryBean proxy, String serviceUrl, Class<?> serviceClass) {
		if(proxy == null) {
			proxy = new JaxWsProxyFactoryBean();
			proxy.setAddress(serviceUrl);
			proxy.setServiceClass(serviceClass);
		}
	
		return proxy.create();
	}
}
